package org.vserver.util;

import org.apache.log4j.Logger;
import sun.misc.BASE64Encoder;

import java.security.MessageDigest;

/**
 * User: karpden ( Denis Karpov )
 * Date: 3/18/11
 * Time: 4:41 PM
 */
public class CryptoUtil {
    private static final Logger log = Logger.getLogger(CryptoUtil.class);

    public static boolean checkPwd(String pwd, String hash){
        return hash.equals(createHash(pwd));
    }
    public static String createHash(String pwd){
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA");
            md.update(pwd.getBytes("UTF-8"));
        } catch (Exception e) {
            log.error(e);
        }
        byte raw[] = md.digest();
        String hash = (new BASE64Encoder()).encode(raw);
        return hash;
    }
}
